﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Arim.S4.Client.Base.Dto
{

    public enum SortType
    {
        Asc = 1,
        Desc = 2,
    }

    /// <summary>
    /// Paged list
    /// </summary>
    /// <typeparam name="T">T</typeparam>
    [Serializable]
    public class PagedList<T> where T : class, new()
    {
        public IList<T> Items { get; set; }
        public int TotalCount { get; set; }
    }

    public static class EnumerableExtensions
    {
        public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, string orderByExpression, bool isAsc) where T : class, new()
        {
            if (string.IsNullOrEmpty(orderByExpression))
                return list;
            try
            {
                PropertyInfo prop = typeof(T).GetProperty(orderByExpression);
                if (isAsc)
                    return list.OrderBy(x => prop.GetValue(x, null));
                else
                    return list.OrderByDescending(x => prop.GetValue(x, null));
            }
            catch
            {
                return list;
            }
        }
    }
}
